from django.db import models


# Create your models here.
class UserInfo(models.Model):
    '''用户表'''
    username = models.CharField("用户名", max_length=32, unique=True)
    password = models.CharField("密码", max_length=128, )
    position_choice = ((1, "运维"), (2, "开发"), (3, "测试"))
    position = models.IntegerField("职位", choices=position_choice)
    email = models.EmailField("邮箱", max_length=128, unique=True)
    isActive = models.BooleanField("在职", default=0)
    department = models.CharField("部门", max_length=64)  # 可以再设置一个部门表


class MainHost(models.Model):
    '''主机列表'''
    hostIP = models.CharField("IP地址", max_length=32)
    hostName = models.CharField("主机名字", max_length=64)
    sysVersion = models.CharField("系统版本", max_length=32)
    username = models.CharField("用户名", max_length=32)
    sshPort = models.CharField("SSH端口", max_length=6)
    note = models.CharField("备注", max_length=128)


class Project(models.Model):
    '''项目表'''
    name = models.CharField("项目名", max_length=64)
    function = models.CharField("功能", max_length=128)
    header = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="负责人")
    developer = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="开发者")
    test_dev = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="测试")
    devOps = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="运维")
    server_host = models.CharField("后端主机", max_length=64)
    nginx_mainhost = models.CharField("Nginx主机", max_length=64)
    # 配置文件,可以引入object
    nginx_mainhost_dispose_file = models.CharField("Nginx的配置文件", max_length=32)
    path_project = models.CharField("项目存放位置", max_length=128)
    url_git = models.CharField("Git地址", max_length=128)


class CommandList(models.Model):
    '''命令表'''
    command = models.CharField("命令", max_length=128)
    time = models.CharField("时间", max_length=64)
    executor = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="执行人")
    describe = models.CharField("描述", max_length=128)
    execute_result = models.CharField("结果", max_length=64)
    host_list = models.CharField("host列表", max_length=200)
    note = models.CharField("备注", max_length=128)


class PlanTaskList(models.Model):
    '''计划任务'''
    time = models.CharField("时间", max_length=64)
    name = models.CharField("名称", max_length=64)
    describe = models.CharField("描述", max_length=128)
    creator = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="添加人")
    host_list = models.CharField("host列表", max_length=200)
    task = models.CharField("任务", max_length=128)
    execute_user = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="执行用户")


class IssueList(models.Model):
    '''发布表'''
    time = models.CharField("时间", max_length=64)
    issue_user = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="发布人")
    version = models.CharField("版本号", max_length=32)
    project_name = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name="项目名")
    issue_choice = (
        (1, "发布中"), (2, "发布失败"),
        (3, "发布成功"), (4, "等待发布"),
        (5, "回滚成功"), (6, "回滚失败"),
        (7, "等待测试"), (8, "测试通过"),
    )
    issue_state = models.IntegerField("发布状态", choices=issue_choice)
    path_issue_file = models.CharField("存放位置(上传文件)", max_length=128)
    path_backup = models.CharField("备份位置", max_length=128)
    issue_type = models.CharField("发布类型", max_length=64)
    isBackup = models.CharField("是否备份", max_length=4)


class MainIssueList(models.Model):
    '''主机发布表'''
    host = models.CharField("主机", max_length=64)
    issue = models.CharField("发布", max_length=32)
    state = models.CharField("状态", max_length=32)
    time = models.CharField("时间", max_length=32)


class InitList(models.Model):
    '''初始化'''
    name = models.CharField("名字", max_length=32)
    path_playbook = models.CharField("playbook的位置", max_length=128)
    time = models.CharField("时间", max_length=64)
    creator = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="创建人")


class InitLogList(models.Model):
    '''初始化日志'''
    init_name = models.CharField("初始化名称", max_length=64)
    host = models.CharField("主机", max_length=32)
    creator = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="创建人")
    time = models.CharField("时间", max_length=32)
    result = models.CharField("结果", max_length=64)
